<template>
  <div class="container">
    <div class="search-box">
      <el-input v-model="search" placeholder="请输入内容" class="input-box"></el-input>
      <el-button @click="closeSearch">清除</el-button>
      <el-button type="primary" @click="searchList">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="fromData">
      <el-table-column type="index" :index="(params._page - 1) * params._limit + 1" label="任务编号" width="80"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="updateDialog(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="del(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        @current-change="handleCurrentChange"
        layout="total, prev, pager, next"
        :total="total"
        :page-size="params._limit"
        :current-page="params._page"
      >
      </el-pagination>
      <el-button size="small" plain @click="openDialog">+ 新增</el-button>
    </div>

    <el-dialog :title="this.ruleForm.id ? '编辑' : '新增'" :visible="dialogVisible" width="30%" @close="closeDialog">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="ruleForm.des"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getListAPI, postListAPI, getUpdateListAPI, updateListAPI, delListAPI, searchListAPI } from '@/api/list'
import { v4 as uuidv4 } from 'uuid'

export default {
  data () {
    return {
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },

      fromData: [],

      total: 0,

      dialogVisible: false,

      ruleForm: {
        name: '',
        des: '',
        id: ''
      },

      rules: {
        name: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
        des: [{ required: true, message: '请输入任务描述', trigger: 'blur' }]
      },

      search: ''
    }
  },

  mounted () {
    this.getList()
  },

  methods: {
    async getList () {
      const res = await getListAPI(this.params)
      // console.log(res)
      this.fromData = res.data
      this.total = Number(res.headers['x-total-count'])
    },

    handleCurrentChange (val) {
      // console.log(val)
      this.params._page = val
      this.getList()
    },

    openDialog () {
      this.dialogVisible = true
    },

    closeDialog () {
      this.dialogVisible = false
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {
        name: '',
        des: '',
        id: ''
      }
    },

    confirm () {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          // console.log(this.ruleForm)
          if (this.ruleForm.id) {
            await updateListAPI(this.ruleForm)
            this.$message.success('修改成功')
          } else {
            this.ruleForm.id = uuidv4()
            this.params._page = Math.ceil(++this.total / this.params._limit)
            await postListAPI(this.ruleForm)
            this.$message.success('添加成功')
          }
          this.closeDialog()
          this.getList()
        } else {
          this.$message.error('任务名称或者任务描述有误')
        }
      })
    },

    async updateDialog (id) {
      this.dialogVisible = true
      // console.log(id)
      const res = await getUpdateListAPI(id)
      // console.log(res)
      this.ruleForm = res.data
    },

    async del (id) {
      await delListAPI(id)
      this.$message.success('删除成功')
      if (this.fromData.length === 1) {
        this.params._page--
        if (this.params._page <= 0) {
          this.params._page = 1
        }
      }
      this.getList()
    },

    async searchList () {
      const res = await searchListAPI(this.search)
      // console.log(res)
      this.fromData = res.data
    },

    closeSearch () {
      this.search = ''
      this.getList()
    }

  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
